package org.wkg.lesson04.lesson10;

import java.util.concurrent.atomic.AtomicInteger;

public class Demo_1001 {

    public static void main(String[] args) throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                atomicInteger.getAndIncrement();
            }
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                atomicInteger.getAndIncrement();
            }
        });


        t2.start();
        //使用join是因为代码是顺序执行的，在多线程中，会随着虽然会把多线程任务执行完，但是我们在打印atomicInteger值的时候多线程的任务并没有执行完
        t1.join();
        t2.join();

        System.out.println(atomicInteger.get());
    }
}
